package pierwszy;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.StringTokenizer;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.TakesScreenshot;



public class Utils {
	private WebDriver driver;
	private WebDriverWait wait;
	
	Utils(WebDriver driver, WebDriverWait wait){
		this.driver = driver;
		this.wait = wait;
	}
	
    public static String getCurrentDate(){
    	Date currentDate = new Date();
		SimpleDateFormat sDF = new SimpleDateFormat("yyyy-MM-dd, kk:mm:ss");
		
		return sDF.format(currentDate);
    }

	public void mouseActions() throws InterruptedException{
		WebElement przyciskWyczyscNowyPodatek = driver.findElement(UiMapping.wyczyscNowyPodatekBtn);
		
		Actions builder = new Actions(driver);
		//builder.moveByOffset(przyciskWyczyscNowyPodatek.getLocation().getX() + 1, przyciskWyczyscNowyPodatek.getLocation().getY() + 1).click();
		
		//clickAndHold, release
		builder.clickAndHold(przyciskWyczyscNowyPodatek);
		Thread.sleep(2000);
		builder.release(przyciskWyczyscNowyPodatek);
		builder.perform();
		
		Thread.sleep(3000);
		
		//dragAndDrop
		wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//img[contains(@alt,'Wybierz datę wygaśniecia wpisu')]")));
		WebElement commissionCalendar = driver.findElement(By.xpath("//img[contains(@alt,'Wybierz datę wygaśniecia wpisu')]"));
		
		wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div/input[@name='data']")));
		WebElement iFrame = driver.findElement(By.xpath("//div/input[@name='data']"));

		builder.dragAndDrop(commissionCalendar, iFrame);
		builder.build().perform();
		
		Thread.sleep(3000);
		
		//double click
		wait.until(ExpectedConditions.presenceOfElementLocated(By.name("id_cat")));
		WebElement taxType = driver.findElement(By.name("id_cat"));
		
		builder.doubleClick(taxType).perform();
		
		Thread.sleep(3000);
		
		//context click
		builder.contextClick(taxType).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).click().perform();
		
		
		//keyboard
		//builder.keyDown(iFrame, Keys.SHIFT).sendKeys("z").keyUp(Keys.SHIFT).perform();
		//builder.keyDown(iFrame, Keys.ALT).sendKeys("z").keyUp(Keys.ALT).perform();
		//builder.sendKeys(Keys.chord(Keys.ALT + "x")).perform();
		iFrame.sendKeys(Keys.chord(Keys.ALT + "z"));
	}
	
	public void makeScreenShoot() throws IOException{
		Date currentDate = new Date();
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		
		File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

		FileUtils.copyFile(scrFile, new File("C:/Documents and Settings/Wojtek/Pulpit/Selenium/ScreenShots/seleniumscreenShot" + df.format(currentDate) + ".png"),true);
	}
	
	public void navigateSite(){
		driver.navigate().back();
		driver.navigate().forward();
		driver.navigate().refresh();
		driver.navigate().to("www.google.pl");
	}
	
	public void gmailLogin(String lgn, String paswd){
		WebElement login;
		WebElement password;
		
		wait.until(ExpectedConditions.presenceOfElementLocated(By.id("email")));
		login = driver.findElement(By.id("email"));
		
		wait.until(ExpectedConditions.presenceOfElementLocated(By.id("pass")));
		password = driver.findElement(By.id("pass"));
		
		login.sendKeys(lgn);
		password.sendKeys(paswd);
		
		password.submit();
	}
	
	public WebDriver storeCookies(){
		File f = new File("C:/Documents and Settings/Wojtek/Pulpit/Selenium/Cookies/cookies.data");
		try{
			f.delete();
			f.createNewFile();
			FileWriter fos = new FileWriter(f);
			BufferedWriter bos = new BufferedWriter(fos);
			
			for(Cookie cookie : driver.manage().getCookies()){
				bos.write(
						cookie.getName()   + ";" +
					    cookie.getValue()  + ";" +
						cookie.getDomain() + ";" +
					    cookie.getPath()   + ";" +
						cookie.getExpiry() + ";" +
					    cookie.isSecure()  + ";"
						);
				bos.newLine();
			}
			bos.flush();
			bos.close();
			fos.close();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return driver;
	}
	
	public WebDriver addCookies(){
		try{
		File f = new File("C:/Documents and Settings/Wojtek/Pulpit/Selenium/Cookies/cookies.data");
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String line = "";
		
		while((line=br.readLine()) != null){
			StringTokenizer str = new StringTokenizer(line,";");
			
			while(str.hasMoreTokens()){
				String name = str.nextToken();
				String value = str.nextToken();
				String domain = str.nextToken();
				String path = str.nextToken();
				Date expiry = null;
				String dt;
				if(!(dt=str.nextToken()).equals("null")){
					SimpleDateFormat df = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
					expiry = df.parse(dt);
				}
				boolean isSecure = new Boolean(str.nextToken()).booleanValue();
				Cookie cookie = new Cookie(name, value, domain, path, expiry, isSecure);
				driver.manage().addCookie(cookie);
				
			}
		}
		br.close();
		fr.close();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return driver;
	}
}
